package com.xiaoc.bulldozer.controller;

import com.xc.framework.common.page.PageDto;
import com.xiaoc.bulldozer.common.ResponseCode;
import com.xiaoc.bulldozer.common.dto.AccountDto;
import com.xiaoc.bulldozer.common.dto.AccountQueryDto;
import com.xiaoc.bulldozer.common.model.Account;
import com.xiaoc.bulldozer.facade.AccountFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * describe:
 *
 * @author lihuancheng
 * @date 2018/9/12
 */
@Controller
@RequestMapping(value = "/account")
public class AccountController extends AdminBaseController {

    private Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Resource(name = "accountFacade")
    private AccountFacade accountFacade;


    @GetMapping(value = "/list")
    public ModelAndView list(AccountQueryDto accountQueryDto) {

        Map<String, Object> params = new HashMap<String, Object>();

        int count = accountFacade.getAccountCount(accountQueryDto);

        if (count > 0) {
            List<AccountDto> accountList = accountFacade.getAccountList(accountQueryDto);
            params.put("accountList", accountList);
        }

        params.put("queryDto", accountQueryDto);
        params.put("pageDto", new PageDto(accountQueryDto.getPage(), count, PageDto.PAGE_SIZE));
        return renderHtml("account/list.html", params);
    }

    @PostMapping(value = "/update")
    public ModelAndView update(@RequestBody AccountDto accountDto) {

        if (accountDto.getId() == null) {
            return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "用户ID不能为空！");
        }

        if (accountDto.getType() == null) {
            return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "用户类型不能为空！");
        }

        accountFacade.updateAccount(accountDto);

        return jsonSucess();

    }

    @GetMapping(value = "/gotoUpdate")
    public ModelAndView gotoUpdate(Integer id) {

        if (id == null) {
            return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "用户ID不能为空！");
        }

        Account account = accountFacade.getAccountById(id);
        if (account == null) {
            return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "用户不存在！");
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("account", account);
        params.put("userType", account.getType());
        return renderHtml("account/update.html", params);
    }

    @GetMapping(value = "/export")
    public void exportExcel(AccountQueryDto accountQueryDto, HttpServletRequest request, HttpServletResponse response) {

    }
}
